/**
 * swjoy.com Inc.
 * Copyright (c) 2004-2017 All Rights Reserved.
 */
package com.chongyuan.producer.quickServer;

import java.io.IOException;
import java.net.SocketTimeoutException;

import org.quickserver.net.server.ClientCommandHandler;
import org.quickserver.net.server.ClientHandler;

import com.chongyuan.producer.server.GpsMsgServer;
import com.chongyuan.producer.util.SpringBeanUtil;

/**
 * 
 * @author taof.feng
 * @version $Id: EchoCommandHandler.java, v 0.1 2017年8月31日 上午10:06:54 taof.feng
 *          Exp $
 */
public class GpsCommandHandler implements ClientCommandHandler {

	/**
	 * 关闭连接
	 * 
	 * @see org.quickserver.net.server.ClientEventHandler#closingConnection(org.quickserver.net.server.ClientHandler)
	 */
	@Override
	public void closingConnection(ClientHandler handler) throws IOException {
		handler.closeConnection();

	}

	/**
	 * 客户端连接成功时，自动执行此方法
	 * 
	 * @see org.quickserver.net.server.ClientEventHandler#gotConnected(org.quickserver.net.server.ClientHandler)
	 */
	@Override
	public void gotConnected(ClientHandler handler) throws SocketTimeoutException, IOException {
		// 创建socket连接时，可将用户标识和ClientHandler绑定，放入缓存
		// handler.sendClientMsg("success connected!");
		System.out.println("ip：" + handler.getSocket().getInetAddress().getHostAddress());

	}

	/**
	 * 失去链接
	 * 
	 * @see org.quickserver.net.server.ClientEventHandler#lostConnection(org.quickserver.net.server.ClientHandler)
	 */
	@Override
	public void lostConnection(ClientHandler handler) throws IOException {
		handler.sendSystemMsg("Connection lost : " + handler.getSocket().getInetAddress());
	}

	/**
	 * 接收客户端信息并处理
	 * 
	 * @see org.quickserver.net.server.ClientCommandHandler#handleCommand(org.quickserver.net.server.ClientHandler,
	 *      java.lang.String)
	 */
	@Override
	public void handleCommand(ClientHandler handler, String command) throws SocketTimeoutException, IOException {
		GpsMsgServer gpsMsgServer = SpringBeanUtil.getBean("gpsMsgServer");
		System.out.println("reciveMsg:" + command);
		String returnMsg = gpsMsgServer.dandleMsg(handler, command);
		if (returnMsg != null) {
			handler.sendClientMsg(returnMsg);
			System.out.println("sendMsg:" + returnMsg);
		}
	}

}
